﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FaultBackService.Shared;

namespace FaultBackService.DataHelper
{
    class RealTimeFaultDailyTransaction
    {

        /// <summary>
        /// 指定日期的实时故障数据集合
        /// </summary>
        IList<Models.RealTimeFaultData> _realTimeFaultDataList;
        DateTime _dayOfData;

        public RealTimeFaultDailyTransaction(DateTime dayOfData)
        {
            _dayOfData = dayOfData;
            _realTimeFaultDataList = new BLL.RealTimeFaultData().GetList(dayOfData);
        }

        /// <summary>
        /// 分析数据，返回写入RealTimeFaultDataDaily的记录数
        /// </summary>
        /// <param name="dayOfData">实时故障日期</param>
        /// <returns></returns>
        public int Prase()
        {
            if (_realTimeFaultDataList != null && _realTimeFaultDataList.Count > 0)
            {
                //分析汇总
                IDictionary<RealTimeFaultDataIndex, Dictionary<int, float>> dict = new Dictionary<RealTimeFaultDataIndex, Dictionary<int, float>>();
                foreach (var data in _realTimeFaultDataList)
                {
                    var index = new RealTimeFaultDataIndex(data.FaultId, data.TurbineId);
                    if (!dict.ContainsKey(index))
                    {
                        dict.Add(index, new Dictionary<int, float>() { [data.FaultLevel] = 10f });
                    }
                    else
                    {
                        if (!dict[index].ContainsKey(data.FaultLevel))
                        {
                            dict[index].Add(data.FaultLevel, 10f);
                        }
                        else
                        {
                            dict[index][data.FaultLevel] += 10f;
                        }
                    }
                }

                //根据汇总得到的字典，创建对象
                if (dict.Count > 0)
                {
                    BLL.RealTimeFaultDataDaily bll = new BLL.RealTimeFaultDataDaily();

                    IList<Models.RealTimeFaultDataDaily> list = new List<Models.RealTimeFaultDataDaily>();
                    foreach (var turbine_fault in dict)
                    {
                        float total_FaultLevel1=0f, total_FaultLevel2=0f, total_FaultLevel3=0f;
                        if (turbine_fault.Value.ContainsKey(1))
                        {
                            total_FaultLevel1 = turbine_fault.Value[1];
                        }
                        if (turbine_fault.Value.ContainsKey(2))
                        {
                            total_FaultLevel2 = turbine_fault.Value[2];
                        }
                        if (turbine_fault.Value.ContainsKey(3))
                        {
                            total_FaultLevel3 = turbine_fault.Value[3];
                        }

                        Models.RealTimeFaultDataDaily model = bll.Create(
                            turbine_fault.Key.FaultId, turbine_fault.Key.TurbineId, _dayOfData,
                            total_FaultLevel1, total_FaultLevel2, total_FaultLevel3);
                        list.Add(model);
                    }

                    //写入数据库，返回写入记录数目
                    var count = bll.AddList(list);
                    //写日志
                    new Log().RealTimeFaultDailySuccess(count);
                    return count;
                }
            }

            //写日志,数目为0，中止执行
            new Log().RealTimeFaultDailyStop();
            return 0;
        }
    }

    /// <summary>
    /// 实时故障数据索引
    /// </summary>
    struct RealTimeFaultDataIndex
    {
        public int FaultId;
        public int TurbineId;

        public RealTimeFaultDataIndex(int faultId, int turbineId)
        {
            FaultId = faultId;
            TurbineId = turbineId;
        }
    }
}
